package com.hqyj.service.impl;

import com.hqyj.pojo.BuyCar;
import com.hqyj.pojo.Item;
import com.hqyj.pojo.R;
import com.hqyj.service.BuyCarService;
import com.hqyj.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class BuyCarServiceImpl implements BuyCarService {
    //注入redis
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public R addBuyCar(Item item, HttpServletRequest request) {
        //获取token
        String token = request.getHeader("Authorization");
        //获取用户名
        String userName = JwtUtil.getClaimByName(token,"userName").asString();
        //定义redis的键名(存购物车里的商品id的集合)
        String keyList = "buyCar:list:"+userName;
        //定义redis的键名(存购物车里的数据)
        String keyHash = "buyCar:hash:"+userName;
        //判断购物车里是否有数据
        Object obj = redisTemplate.opsForHash().get(keyHash,item.getGoodsId().toString());
        //判断是新的商品
        if(obj==null){
            log.info("添加新商品");
            //计算商品的金额(数量*单价)
            item.setMoney(item.getNumber()*item.getGoodsPrice());
            //记录商品id的信息
            redisTemplate.opsForList().leftPush(keyList,item.getGoodsId());
            //存入购物车中
            redisTemplate.opsForHash().put(keyHash,item.getGoodsId().toString(),item);
        }else{
            log.info("更改商品的数据");
            //更改商品数量
            Item i = (Item)obj;
            //以前的数值+redis的数值
            i.setNumber(item.getNumber()+i.getNumber());
            //计算商品金额
            i.setMoney(i.getNumber()*i.getGoodsPrice());
            //存入购物车中
            redisTemplate.opsForHash().put(keyHash,item.getGoodsId().toString(),i);
        }

        return R.ok("购买成功");
    }

    @Override
    public R showBuyCar(HttpServletRequest request) {
        //获取token
        String token = request.getHeader("Authorization");
        //获取用户名
        String userName = JwtUtil.getClaimByName(token,"userName").asString();
        //定义redis的键名(存购物车里的商品id的集合)
        String keyList = "buyCar:list:"+userName;
        //定义redis的键名(存购物车里的数据)
        String keyHash = "buyCar:hash:"+userName;
        //获取当前用户的购物车数据的所有商品Id
        Object obj = redisTemplate.opsForList().range(keyList,0,-1);
        if(obj==null){
            log.info("购物车没有数据");
            return R.error("购物车没有数据");
        }
        log.info("购物车取出数据");
        List<Item> itemList = new LinkedList<>();
        //redis中的list数据结构 取值,语法:range(键名，起始索引，截止索引)，如果截止索引是-1，则查询所有
        Object li= redisTemplate.opsForList().range(keyList,0,-1);
        List<Integer> listId =(List<Integer>)li;
        //创建购物车对象
        BuyCar buyCar = new BuyCar();
        //创建总价
        Double totalPrice=0D;
        for(Integer i:listId){
            //取出redis的购物车数据
            Item item =(Item) redisTemplate.opsForHash().get(keyHash,i.toString());
            itemList.add(item);
            //计算总金额
            totalPrice+=item.getMoney();
        }
        //设置到购物车对象里
        buyCar.setItemList(itemList);
        buyCar.setTotalPrice(totalPrice);
        return R.ok("success",200,buyCar);
    }

    @Override
    public R delBuyCar(Integer goodsId, HttpServletRequest request) {
        //获取token
        String token = request.getHeader("Authorization");
        //获取用户名
        String userName = JwtUtil.getClaimByName(token,"userName").asString();
        //定义redis的键名(存购物车里的商品id的集合)
        String keyList = "buyCar:list:"+userName;
        //定义redis的键名(存购物车里的数据)
        String keyHash = "buyCar:hash:"+userName;
        //redis的list数据结构删除某一个数据：remove(reids键名,删除几条数据,删除的数据)
        //删除list里的数据
        redisTemplate.opsForList().remove(keyList,0,goodsId);
        //删除hash里的数据
        Long num = redisTemplate.opsForHash().delete(keyHash,goodsId.toString());
        if(num>0){
            log.info("删除成功");
            return R.ok("删除成功");
        }
        log.info("删除失败");
        return R.error("删除失败");


    }

    @Override
    public R updateBuyCar(BuyCar buyCar, HttpServletRequest request) {
        log.info("-------开启修改购物车业务------------");
        //获取token
        String token = request.getHeader("Authorization");
        //获取用户名
        String userName = JwtUtil.getClaimByName(token,"userName").asString();
        //定义redis的键名(存购物车里的商品id的集合)
        String keyList = "buyCar:list:"+userName;
        //定义redis的键名(存购物车里的数据)
        String keyHash = "buyCar:hash:"+userName;
        //获取所有商品Id
        Object objId = redisTemplate.opsForList().range(keyList,0,-1);
        List<Integer> listId =(List<Integer>)objId;
        log.info("第一步：删除所有的数据");
        for(Integer id:listId){
            //删除list
            redisTemplate.opsForList().remove(keyList,0,id);
            //删除hash
            redisTemplate.opsForHash().delete(keyHash,id.toString());
        }
        log.info("第二步：添加所有的数据");
        for(Item i:buyCar.getItemList()){
            //添加list
            redisTemplate.opsForList().leftPush(keyList,i.getGoodsId());
            //添加hash
            redisTemplate.opsForHash().put(keyHash,i.getGoodsId().toString(),i);
        }
        log.info("-------修改购物车业务完毕------------");
        return R.ok();
    }
}
